package com.itheima.cs;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.itheima.cs.common.dyewm.PrintQRCodeActivity;
import com.itheima.cs.common.nbdb.InternalTransferActivity;
import com.itheima.cs.common.wwdb.OutsourcingAllocationActivity;
import com.itheima.cs.common.wwth.OutsourcedReturnsActivity;
import com.itheima.cs.utils.CommonUtils;
import com.itheima.cs.utils.MyApplication;

public class HomeFragmentActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton button_nbdb;
    private ImageButton button_wwdbth;
    private ImageButton button_wwdb;
    private ImageButton button_dyewm;

    private boolean isExit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_fragment);
        initView();


        MyApplication app = MyApplication.getInstance();
        if (!app.isSearching()) {
            app.startGlobalSearch();
        }

    }

    private void initView(){

        button_nbdb = findViewById(R.id.button_nbdb);
        button_wwdbth = findViewById(R.id.button_wwdbth);
        button_wwdb = findViewById(R.id.button_wwdb);
        button_dyewm = findViewById(R.id.button_dyewm);


        button_nbdb.setOnClickListener(this);


        button_wwdbth.setOnClickListener(this);
        button_wwdb.setOnClickListener(this);
        button_dyewm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_nbdb:{
                Intent intent = new Intent();
                String model = Build.MODEL; // 获取设备型号
                if (model.startsWith("CT58")){
                    //扫码机器优博讯CT58
                    boolean checkWifi = CommonUtils.checkWifiInfo(HomeFragmentActivity.this);
                    if (!checkWifi) {
                        //优博讯CT58需要检验WiFi，不是TSZG开头的wifi
                        String errorMessage = "设备连接得无线网不正确，请连接正确的热点（热点名称以TSZG开头）。";
                        Toast.makeText(HomeFragmentActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
                        return;
                    }
                    intent.setClass(HomeFragmentActivity.this, ScanManagerDemo.class);
                } else if (model.startsWith("V2s")){
                    //SunMiV2s赋码设备
                    intent.setClass(HomeFragmentActivity.this, InternalTransferActivity.class);
                } else {
                    String errorMessage = "非指定设备，禁止使用";
                    Toast.makeText(HomeFragmentActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(intent);
                break;
            }


            case R.id.button_wwdbth:{
                Intent intent = new Intent();
                intent.setClass(HomeFragmentActivity.this, OutsourcedReturnsActivity.class);
                startActivity(intent);
                break;
            }



            case R.id.button_wwdb:{
                Intent intent = new Intent();
                intent.setClass(HomeFragmentActivity.this, OutsourcingAllocationActivity.class);
                startActivity(intent);
                break;
            }

            case R.id.button_dyewm:{
                Intent intent = new Intent();
                intent.setClass(HomeFragmentActivity.this, PrintQRCodeActivity.class);
                startActivity(intent);
                break;
            }
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1001) {
            boolean allGranted = true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false;
                    break;
                }
            }
            MyApplication.getInstance().onPermissionsResult(allGranted);
        }
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(HomeFragmentActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isExit = false;
                    }
                }, 2000);
            } else {
                finishAffinity();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}



